/**
 * Copyright (c) 2018-2028, Joffee.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sailmi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sailmi.common.controller.BaseController;
import com.sailmi.common.constant.RoleConstant;
import com.sailmi.common.model.auth.AuthUser;
import com.sailmi.common.mybatis.support.Condition;
import com.sailmi.common.mybatis.support.Query;
import com.sailmi.common.secure.annotion.PreAuth;
import com.sailmi.common.tool.api.R;
import com.sailmi.common.tool.utils.Func;
import com.sailmi.system.entity.SystemEntity;
import com.sailmi.system.service.ISystemService;
import com.sailmi.system.vo.AuthClientVo;
import com.sailmi.system.wrapper.AuthClientWrapper;
import com.sailmi.system.entity.AuthClient;
import com.sailmi.system.service.IAuthClientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 *  应用管理控制器
 *
 * @author Joffee
 */
@RestController
@RequestMapping("/client")
@Tag(name = "应用管理", description = "应用管理接口")
@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public class AuthClientController extends BaseController {
	@Autowired
	private IAuthClientService clientService;
	@Autowired
	private ISystemService systemService;

	/**
	* 详情
	*/
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入client")
	public R<AuthClient> detail(AuthClient authClient) {
		AuthClient detail = clientService.getOne(Condition.getQueryWrapper(authClient));
		return R.data(detail);
	}

	/**
	* 分页
	*/
	@GetMapping("/list")
	@PreAuth(RoleConstant.HAS_ROLE_TENANT_ADMIN)
	@Operation(summary = "分页", description = "传入client")
	public R<IPage<AuthClientVo>> list(AuthUser user, AuthClient authClient, Query query) {
		ArrayList<Long> systemIds = new ArrayList<>();
		if(user!=null && user.getTenantId()!=null) {
			QueryWrapper<SystemEntity> systemEntityQueryWrapper = new QueryWrapper<>();
			if(user.getTenantId().equals("000000")){

			}else{
				systemEntityQueryWrapper.eq("tenant_id",user.getTenantId());
			}

			List<SystemEntity> list = systemService.list(systemEntityQueryWrapper);
			if(list!=null && list.size()>0){
				list.stream().forEach(sysEntity->{
					systemIds.add(sysEntity.getId());
				});
			}
		}
		QueryWrapper<AuthClient> queryWrapper = Condition.getQueryWrapper(authClient);
		if(systemIds!=null && systemIds.size()>0){
			queryWrapper.in("system_id",systemIds);
		}
		IPage<AuthClient> pages = clientService.page(Condition.getPage(query), queryWrapper);
		IPage<AuthClientVo> serviceVOIPage = AuthClientWrapper.build().pageVO(pages);
		if(serviceVOIPage!=null && serviceVOIPage.getTotal()>0){
			serviceVOIPage.getRecords().stream().forEach(clientVo->{
				clientVo.setSystemName(systemService.getById(clientVo.getSystemId()).getSystemName());
			});
		}
		return R.data(serviceVOIPage);
	}

	/**
	* 新增
	*/
	@PostMapping("/save")
	@Operation(summary = "新增", description = "传入client")
	public R save(@Valid @RequestBody AuthClient authClient) {
		return R.status(clientService.save(authClient));
	}

	/**
	* 修改
	*/
	@PostMapping("/update")
	@Operation(summary = "修改", description = "传入client")
	public R update(@Valid @RequestBody AuthClient authClient) {
		return R.status(clientService.updateById(authClient));
	}

	/**
	* 新增或修改
	*/
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入client")
	public R submit(@Valid @RequestBody AuthClient authClient) {
		return R.status(clientService.saveOrUpdate(authClient));
	}


	/**
	* 删除
	*/
	@PostMapping("/remove")
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return R.status(clientService.deleteLogic(Func.toLongList(ids)));
	}


}
